﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Grishko.DataTier.SqlLibrary
{
    public class Like : SqlWord
    {

        private string GetParametrsAsString()
        {   
            if(Paramentrs.Count ==0)
                    return string.Empty ;
            
            StringBuilder sb = new StringBuilder();
            
            for (int i=0; i < Paramentrs.Count;i++ )
            {
                string st = string.Empty;
                if (Paramentrs[i].isBetweenParametr)
                    st = Paramentrs[i].SQLValue;
                else
                    st = ValueTxt + " like " + Paramentrs[i].SQLValue;
     
                sb.AppendFormat((i == 0 ? "" : " or ")+st );
            }
             // dbo.CompareValueText_Between(ValueTxt,'"+s2+"','"+ s3 + "')=1)";
            

            return "("+sb.ToString()+")";
        
        }

        

        /// <summary>
        /// Пример результата
        /// (ParamTxt like '%конструкция%' and 	( ValueTxt like '%щит%'  or  ValueTxt like '%6Х3%' 	))
        /// </summary>
        /// <returns></returns>
        public override string GetSql()
        {
            //Подготовить коллекцию параметров
            Prepare();


            if(ParamTxtValue.Length ==0)
                    return GetParametrsAsString();


            return "(" + ParamTxt + " like '%" + ParamTxtValue + "%' and " + GetParametrsAsString() + ")";
            
        }

        /// <summary>
        /// Сформировать коллекцию параметров
        /// </summary>
        protected override void Prepare()
        {
            ParamString = ParamString.Trim();
            base.Prepare();
            int StartParamIdx = 0;
            StartParamIdx = ParamString.IndexOf("=");
            string ParamSource = ParamString.Substring(StartParamIdx==-1?0:StartParamIdx+1);
            foreach (string item in ParamSource.Split(ParamSplit))
            {
                Paramentrs.Add(new WordParametr(item));
            }
        }

        public override SqlWord.SqlWordType WordType
        {
            get
            {
                string DoNotLike        = @"\B[!=]{1}";
                string Like             = @"\B[=]{1}";                
                
                Regex r = new Regex(DoNotLike);                
                if (r.IsMatch (ParamString)) 
                            return SqlWordType .DoNotLike ;

                r = new Regex(Like);                
                if (r.IsMatch (ParamString)) 
                           return SqlWordType .Like  ;

                //r = new Regex(NotParamLike);
                //if (r.IsMatch(ParamString)) return SqlWordType.NotParamLike ;
                
                if (ParamString.IndexOf(",") > -1)
                           return SqlWordType.NotParamLike;
                
                 return SqlWordType.Like;
                
            }
        }

        public int ParamTxtValueIndex 
        {
            get 
            {
                if (ParamString.IndexOf("=") == -1)
                    return 0;

                return ParamString.IndexOf("!=") > -1 ? ParamString.IndexOf("!=") : ParamString.IndexOf("="); 
            }   
        }

        /// <summary>
        /// Значение параметра
        /// </summary>
        protected override string ParamTxtValue
        {
            get
            {
                return ParamString.Substring (0, ParamTxtValueIndex);
            }
        }
    }
}
